package wechat

import (
	"fmt"
	"hft/db/mongodb/model/wechat"
	"hft/tools"
	"hft/tools/weixin/api"
	"hft/tools/weixin/model"

	weserve "hft/Http/Services/wechat"
	utils "hft/Http/Utils"

	"github.com/gin-gonic/gin"
)

type WeChat struct{}

func (*WeChat) CreateMenu(c *gin.Context) {
	var data *model.CustomMenu
	c.ShouldBindJSON(&data)
	err := weserve.WeChatService.CreateMenu(data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*WeChat) GetMenu(c *gin.Context) {
	list, err := weserve.WeChatService.GetMenu()
	if err != nil {
		fmt.Println(err)
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.OkWithData(list, c)
}

// 获取素材列表
func (*WeChat) GetSuCaiList(c *gin.Context) {
	var req *model.SuCaiReq
	c.ShouldBindJSON(&req)

	data, err := api.WeiXinSuCai.List(req.Type, req.Offset, req.Count)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

// 获取素材总数
func (*WeChat) GetSuCaiCount(c *gin.Context) {
	list, err := api.WeiXinSuCai.Count()
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)
}

// 获取草稿
func (*WeChat) GetDraftList(c *gin.Context) {
	var req *model.DraftReq
	c.ShouldBindJSON(&req)

	data, err := api.WeiXinSuCai.DraftList(req.NoContent, req.Offset, req.Count)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

// 获取草稿总数
func (*WeChat) GetDraftCount(c *gin.Context) {
	list, err := api.WeiXinSuCai.DraftCount()
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)
}

// 获取永久素材
func (*WeChat) GetForEver(c *gin.Context) {
	var data *model.Media
	c.ShouldBindJSON(&data)

	res, err := api.WeiXinSuCai.GetForEver(data.MediaID, data.MaterialType)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(res, c)
}

// 关注时回复
func (*WeChat) SubscribeWhenReply(c *gin.Context) {
	var data *wechat.SubscribeReply
	c.ShouldBindJSON(&data)
	fmt.Println(tools.JsonParse(data))
	err := weserve.WeChatService.ReplyWhenSubscribe(*data)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

// 获取回复
func (*WeChat) GetSubscribeReply(c *gin.Context) {
	data, err := weserve.WeChatService.GetSubscribeReply()
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

// 获取模板列表
func (*WeChat) GetTemplateList(c *gin.Context) {
	data, err := api.WeiXinTemplate.List()
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(data, c)
	}
}

var WeChatController = new(WeChat)
